<?php
namespace jy_starea;
defined('BY_JYA') or exit('error');
/*
	分销插件工具类
*/
class model extends \plugin{
	
	
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function available_areas($isid=0,$noone=false){
		$areas=file_get_contents(ROOT_D.'/core/resource/area_town.txt');
		$areas=unserialize($areas);
		if($isid){
			$newarea=array();
			if($noone){
				$newarea[]=array('title'=>'请选择省','list'=>array());
			}
			foreach($areas as $province=>$pv){
				$newpv=array();
				if($noone){
					$newpv[]=array('title'=>'请选择城市','list'=>array());
				}
				foreach($pv as $city=>$cv){
					$newcv=array();
					if($noone){
						$newcv[]=array('title'=>'请选择区','list'=>array());
					}
					foreach($cv as $district=>$dv){
						$newdv=array();
						if($noone){
							$newdv[]=array('title'=>'请选择街道','list'=>array());
						}
						foreach($dv as $street=>$sv){
							$newdv[]=array(
								'title'=>$street,
								'list'=>$sv
							);
						}
						$newcv[]=array(
							'title'=>$district,
							'list'=>$newdv
						);
					}
					$newpv[]=array(
						'title'=>$city,
						'list'=>$newcv
					);
				}
				$newarea[]=array(
					'title'=>$province,
					'list'=>$newpv
				);
			}
			$areas=$newarea;
		}
		return $areas;
	}
	public function get_area_code($area){
		$address=$area['province'];
		$oldaddress=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
			$oldaddress.=' 直辖市';
		}
		if($area['district']){
			$address.=' '.$area['district'];
			$oldaddress.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
			$oldaddress.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if(!$isup){
				require ROOT_D.'/update/update_area.php';
				$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
			}else{
				$area_code=pdo_getcolumn('core_area',array('address'=>$oldaddress),'code');
				if(!$area_code){
					pdo_query('DROP TABLE IF EXISTS '.TABLE_PRE.'core_area;');
					require ROOT_D.'/update/update_area.php';
				}
			}
		}
		return $area_code;
	}
}